package com.congee02.sw.var;

import java.util.*;

public class MaximumUniqueSubarray {

    public int maximumUniqueSubarray(int[] nums) {

        int n = nums.length;

        boolean[] exist = new boolean[10001];

        // l = r = 0
        exist[nums[0]] = true;
        int sum = nums[0];

        int ans = sum;

        int l = 0;
        for (int r = 1 ; r < n ; r ++ ) {
            // 合法化窗口之二:
            // 1. 为新加入的元素创造条件
            // 2. 加入新元素并处理
            while (exist[nums[r]]) {
                int remove = nums[l ++];
                sum -= remove;
                exist[remove] = false;
            }
            sum += nums[r];
            exist[nums[r]] = true;
            ans = Math.max(ans, sum);
        }

        return ans;
    }

}
